7bit \ 8bit encoding functions

'_______________________By Kev Peel_________________________
'
'  7 bit <> 8bit Encoding Functions
'  --------------------------------
'
'  These two functions will encode and decode 7 bit data to\from 8 bit.
'  Pointers are used for maximum efficiency.
'
'  KGP Software, 2004.
'_____________________________________________________________
 
#Compile Exe
#Dim All
 
#Include "WIN32API.INC"
  
'-------------------------------------------------------------
'  Encode 8-bit data to 7-bit. 
'  ASCII string with encoded characters is returned.
'-------------------------------------------------------------
Function Encode8bitTo7Bit(ByVal sData As String) As String
  Local p As Byte Ptr, b As Byte Ptr, i As Long, x As Long
  Local sBuffer As String, count As Long, filter As Byte
  Local mask As Byte, newadd As Byte
 
  ' Remove invalid characters, these cannot be encoded...
  sData = Remove$(sData, Any Chr$(128 To 255))
 
  ' Get data pointer...
  p = StrPtr(sData)
 
  ' Allocate buffer...
  sBuffer = String$(Len(sData) - (Len(sData)\8), 0)
  b = StrPtr(sBuffer)
 
  filter = &B00000001
  For i = 0 To Len(sData)-1
 
      @b[count] = @p[i]
      Shift Right @b[count], x
 
      If (i < Len(sData)+1) Then
         newadd = (@p[i+1] And filter)
         Rotate Right newadd, x+1
         @b[count] = @b[count] Or newadd
      End If
     
      Incr x
      Incr count
    
      Bit Set filter, x
   
      If x = 7 Then
         x = 0
         filter = &B00000001
         Incr i
      End If
  Next i
  
  Function = sBuffer

 End Function
'-------------------------------------------------------------
'  Decode 7-bit data to 8-bit. 
'  The result is returned as a standard ASCII string.
'-------------------------------------------------------------
Function Decode7bitTo8Bit(ByVal sData As String) As String
   
  Local p As Byte Ptr, b As Byte Ptr, i As Long, filter As Byte
  Local newmask As Byte, x As Long, c As Long, sBuffer As String
  Local mask As Byte, count As Long
   
  ' Get data pointer...
  p = StrPtr(sData)
   
  ' Ensure buffer has one extra byte for every 7 encoded bytes...
  sBuffer = String$(Len(sData) + (Len(sData) \ 8) + 1, 0)
  b = StrPtr(sBuffer)
       
  ' Loop through and decode the 7bit data
  ' We must ensure we have a big enough output
  ' buffer as the expanded (8bit) data is larger...
  filter = &B11111111
  mask = &B00000000
      
  For i = 0 To Len(sData)-1
     
    Bit Reset filter, 7-x
    
    newmask = &B00000000
    For c = 0 To x
      If Bit(@p[i], 7-c) Then Bit Set newmask, 7-c Else Bit Reset newmask, 7-c
    Next c
   
    @b[count] = ((@p[i] And filter) Or mask)
    Rotate Left @b[count], x
  
    mask = newmask
    Incr x
    Incr count
 
    If x = 7 Then
 
       ' Save extra byte...
       @b[count] = mask
       Rotate Left @b[count], x
       Incr count

' Reset counter and bitmask values...
x = 0
filter = &B11111111
mask = &B00000000
End If

Next i

' Return the string, remove trailing NULLs...
Function = RTrim$(sBuffer, $Nul)
End Function

------------------
email
http://www.kgpsoftware.com/


